const PublicModel = require("@/common/public.model");
const { TenantContext } = require("@/core/tenant-context");

/**
 * 单据模型
 */
class OrderModel extends PublicModel {
    /**
     * 获取桌台规则列表
     * @param {*} param0 
     * @returns 
     */
    getGameTableRuleList({ table_id }) {
        const SQL = this.whereAnd(`
            SELECT 
                r.id,
                r.rule_name,
                r.rule_type,
                r.start_time,
                r.end_time,
                r.min_consumption,
                r.max_duration,
                r.base_fee,
                r.base_hours,
                r.fixed_price,
                r.fixed_duration,
                r.fixed_fee,
                r.weekdays,
                r.priority,
                r.effective_date,
                r.expire_date 
            FROM 
                game_table_billing_rules br 
                LEFT JOIN game_billing_rules r ON br.rule_id = r.id 
        `, {
            'br.table_id': table_id,
            'r.tenant_id': TenantContext.getCurrentTenant(),
            'r.is_delete': 0
        });
        return this.query(SQL);
    }

    /**
     * 获取收费规则
     * @param {*} id 
     * @returns 
     */
    getGameBillingRule(id) {
        return this.findById({ table: 'game_billing_rules', id: id });
    }

    /**
     * 创建桌台订单
     * @param {*} data 
     * @returns 
     */
    createTableOrder(data) {
        return this.create({ table: 'orders', data, result: true });
    }

    /**
     * 创建桌台计费规则表
     * @param {*} data 
     * @returns 
     */
    createTableOrderBillingRule(data) {
        return this.create({ table: 'order_billing_rules', data });
    }

    /**
     * 创建零售订单
     * @param {*} data 
     * @returns 
     */
    createRetailOrder(data) {
        return this.create({ table: 'orders', data, result: true })
    }

    /**
     * 查询订单
     * @param {*} id 
     * @returns 
     */
    getOrderById(id) {
        return this.findById({ table: 'orders', id: id });
    }

    /**
     * 查询桌台
     * @param {*} id 
     * @returns 
     */
    getTableById(id) {
        return this.findById({ table: 'game_tables', id: id });
    }

    /**
     * 查询商品列表
     * @param {*} idList 
     * @returns 
     */
    queryProductList(idList) {
        return this.findById({ table: 'products', id: idList });
    }

    /**
     * 查询订单下的所有商品
     * @param {*} order_id 
     * @returns 
     */
    queryOrderProductList(order_id) {
        const SQL = this.injectTenantCondition(`SELECT id, product_id, item_name, quantity, unit_price, total_price FROM order_items WHERE order_id = '${order_id}'`);
        return this.query(SQL);
    }

    /**
     * 创建零售订单商品
     * @param {*} data 
     * @returns 
     */
    createRetailOrderProducts(data) {
        return this.createMultiple({table: 'order_items', data: data })
    }

    /**
     * 创建桌台订单商品
     * @param {*} data 
     * @returns 
     */
    createTableOrderProducts(data) {
        return this.createMultiple({table: 'order_items', data: data })
    }
    
    /**
     * 更新桌台状态
     * @param {*} id 
     * @param {*} data 
     * @returns 
     */
    updateTableStatus(id, data) {
        return this.updateById({ table: 'game_tables', id, data });
    }

    /**
     * 更新订单状态
     * @param {*} id 
     * @param {*} data 
     * @returns 
     */
    updateOrderStatus(id, data) {
        return this.updateById({ table: 'orders', id, data });
    }

    /**
     * 获取订单下的规则id
     * @param {*} order_id 
     */
    getOrderBillingRule(order_id) {
        const SQL = `
            SELECT 
                br.* 
            FROM 
                game_billing_rules br 
                LEFT JOIN order_billing_rules obr ON br.id = obr.rule_id 
            WHERE
                br.tenant_id = '${TenantContext.getCurrentTenant()}' 
                AND obr.order_id = '${order_id}' 
                AND br.is_delete = 0
        `;
        return this.query(SQL);
    }
}

module.exports = new OrderModel();
